/*
 * Copyright (c) 2016 Texas Instruments Incorporated - http://www.ti.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  ======== main_tirtos.c ========
 */
#if 0
#include <pthread.h>
#include <semaphore.h>

/* TI Drivers Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/I2C.h>
#include <ti/drivers/SPI.h>
#include <ti/drivers/Power.h>
#include <ti/drivers/crypto/CryptoCC32XX.h>
#include <ti/drivers/net/wifi/simplelink.h>  /* sl_Task */

/* TI RTOS header files */
#include <ti/sysbios/BIOS.h>

/* Example/Board Header files */
#include "custom.h"
#include "Board.h"


/*
 *  ======== main ========
 */
int main(void)
{
    pthread_t cmdThread;
    pthread_attr_t attr;
    struct sched_param priParam;

    /* Call board init functions. */
    Board_initGeneral();



    #if 0
    /* Initialize interrupts for all ports that need them */
    GPIO_setCallback(Board_BUTTON0, gpioButtonFxn0);

    /* Enable interrupts */
    GPIO_enableInt(Board_BUTTON0);



    /* Create the Simplelink spawn thread */
    pthread_attr_init(&attr);
    priParam.sched_priority = 9;
    pthread_attr_setschedparam(&attr, &priParam);
    pthread_attr_setstacksize(&attr, 2048);
    pthread_create(&slSpawnThread, &attr, sl_Task, NULL);
    #endif
    /* Create thread for HAP server */
    //pthread_attr_init(&attr);
    //pthread_attr_setstacksize(&attr, (5 * 1024));
    //pthread_create(&serverThread, &attr, serverFxn, NULL);

    /* Create semaphores for server stop button handling */
    //sem_init(&btnSem, 0, 0);
    //sem_init(&stopSem, 0, 0);

    /* Create thread for server stop button handler */
    //pthread_attr_init(&attr);
    //pthread_attr_setstacksize(&attr, 512);
    //pthread_create(&btnThread, &attr, buttonHandlerFxn, NULL);

    /* Set priority and stack size attributes */
    pthread_attr_init(&attr);
    priParam.sched_priority = 1;
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    pthread_attr_setschedparam(&attr, &priParam);
    pthread_attr_setstacksize(&attr, 4096);
    pthread_create(&cmdThread, &attr, mainThread, NULL);


    /*  To enable low power mode, uncomment the following line.
     *  Please be aware that your JTAG connection will be
     *  dropped when entering low power mode. You must reset the
     *  board in order to re-establish your JTAG connection.
     */
/*  Power_enablePolicy(); */

    /* start the BIOS scheduler */
    BIOS_start();

    /* should never get here */
    return (0);
}

#endif


#include <stdint.h>

/* POSIX Header files */
#include <pthread.h>

/* RTOS header files */
#include <ti/sysbios/BIOS.h>

/* TI-RTOS Header files */
#include <ti/drivers/GPIO.h>

/* Example/Board Header files */
#include "custom.h"
#include "Board.h"

extern void *mainThread(void *arg0);

/* Stack size in bytes */
#define THREADSTACKSIZE    4096

/*
 *  ======== main ========
 */
int main(void)
{
    pthread_t           thread;
    pthread_attr_t      pAttrs;
    struct sched_param  priParam;
    int                 retc;
    int                 detachState;

    /* Call board init functions */
    Board_initGeneral();

    /* Set priority and stack size attributes */
    pthread_attr_init(&pAttrs);
    priParam.sched_priority = 1;

    detachState = PTHREAD_CREATE_DETACHED;
    retc = pthread_attr_setdetachstate(&pAttrs, detachState);
    if (retc != 0) {
        /* pthread_attr_setdetachstate() failed */
        while (1);
    }

    pthread_attr_setschedparam(&pAttrs, &priParam);

    retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    if (retc != 0) {
        /* pthread_attr_setstacksize() failed */
        while (1);
    }

    retc = pthread_create(&thread, &pAttrs, mainThread, NULL);
    if (retc != 0) {
        /* pthread_create() failed */
        while (1);
    }

    BIOS_start();

    return (0);
}

/*
 *  ======== dummyOutput ========
 *  Dummy SysMin output function needed for benchmarks and size comparison
 *  of FreeRTOS and TI-RTOS solutions.
 */
void dummyOutput(void)
{
}

